/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.main;

import com.alibaba.fastjson.JSON;
import com.seari.bean.EmergencyTeam;
import com.seari.bean.EquipmentName;
import com.seari.bean.JqGzFlag;
import com.seari.bean.LineInfo;
import com.seari.bean.Professionxdk;
import com.seari.bean.StationInfo;
import com.seari.bean.ServiceCompany;
import com.seari.bean.StationPosition;
import com.seari.commonutils.redis.JedisUtilProxy;
import com.seari.commonutils.redis.ex.JedisNotInitializeException;
import com.seari.service.EmergencyResourcesService;
import com.seari.service.EquipmentTreeService;
import com.seari.service.LineService;
import com.seari.service.RightLineGuardPointService;
import com.seari.service.StationService;
import com.seari.utils.GuavaCacheUtil;

import redis.clients.jedis.Jedis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * @author liuqi
 */
@Component
@Order(value = 1)
public class StartUpClass implements ApplicationRunner{

    private static final Logger log = LoggerFactory.getLogger(StartUpClass.class);
    
    public static Map<String, StationPosition> stationMap = new HashMap<>();
    
    @Autowired
    private StationService stationService;
    
    @Autowired
    private EquipmentTreeService equipmentTreeService;
    
    @Autowired
    private LineService lineService;

    @Autowired
    private EmergencyResourcesService emergencyResourcesService;
    
    @Autowired
    private RightLineGuardPointService rightLineGuardPointService;

    @Override
    public void run(ApplicationArguments aa){
        log.info("===初始化  地图坐标信息和设备信息  到缓存中===");
        try {
        	//初始化redis 持久化缓存使用
        	JedisUtilProxy.initJedisPoolUtil("127.0.0.1", 6379);
        	//初始化车站坐标配置
        	initStationConfig();
        	//guava 缓存
            init();
		} catch (Exception e) {
			e.printStackTrace();
		}
        log.info("===初始 地图坐标信息和设备信息 完成===");
        
    }
    
    public void init(){
    	//获取车站坐标信息  放入Guava
    	List<StationPosition> stationPositionList = stationService.getStationPositionList();
    	GuavaCacheUtil.put("stationPositionList",stationPositionList);
    	//获取 故障树信息
    	List<EquipmentName> EquipmentNameList = equipmentTreeService.getEquipment();
    	GuavaCacheUtil.put("EquipmentNameList",EquipmentNameList);
    	//获取 故障专业信息
        List<ServiceCompany> getGzZyList = emergencyResourcesService.getGzZyList();
        GuavaCacheUtil.put("getGzZyList",getGzZyList);
        //获取 故障状态信息 （聚群）
        List<JqGzFlag> jqGzFlags = emergencyResourcesService.getJqGzFlags();
        GuavaCacheUtil.put("jqGzFlags",jqGzFlags);
        //获取小电科专业信息（本地mysql profession_fault表  对应关系由用户整理出）
        List<Professionxdk> professionxdkList = rightLineGuardPointService.getGuardPointProfessionFromMysql();
        GuavaCacheUtil.put("professionxdkList",professionxdkList);
        
    	//持久化  聚群车站  线路信息
		Jedis jedis = null;
		List<LineInfo> lineInfoList = lineService.getLineListInfoFromJuqun();
		List<StationInfo> stationInfoList = stationService.getStationInfoList();
//		List<EmergencyTeam> emergencyTeams =  emergencyResourcesService.getEmergencyTeam();
		
		try {
			jedis = JedisUtilProxy.getJedis();
		} catch (JedisNotInitializeException e) {
			e.printStackTrace();
		}
		String lineObjectJson = JSON.toJSONString(lineInfoList); 
		jedis.set("lineInfoList", lineObjectJson);
		
		String stationObjectJson = JSON.toJSONString(stationInfoList); 
		jedis.set("stationInfoList", stationObjectJson);
		
//		String emergencyTeamsJson = JSON.toJSONString(emergencyTeams); 
//		jedis.set("emergencyTeams", emergencyTeamsJson);
		
		jedis.close();
    }
    
    public void initStationConfig() 
    {
    	log.info("init station config>>>>>convert position list to map");
    	List<StationPosition> stationInfos = stationService.getRawStationPosition();
    	stationMap = stationInfos.stream().collect(Collectors.toMap(StationPosition::getName, Function.identity(),(key1,key2) -> key2)); //键冲突时取后来键
    }
}
